package biznussoft.metercor.fragments;

import java.util.HashMap;

import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;

import biznussoft.metercor.HomeActivity;
import biznussoft.metercor.R;
import biznussoft.metercor.lookup.AccountLookup;
import biznussoft.metercor.lookup.ManufacturerLookUp;
import biznussoft.metercor.lookup.ModelLookup;
import biznussoft.metercor.model.Bean_Class;
import biznussoft.metercor.utils.Session;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NewERTDeviceInstallationFragment extends Fragment implements OnClickListener {

	private Button submit;
	public static EditText etCustomer,etModel1,etModel2,etManufacturer;
	private EditText etMeterInformation,etMeter,etSerialNumber,etInspectionDate,etOutputcuftrev,etTemperatureComp,etOriginalMtrSealDate,
	etERTDevice,etMake,etModelERG5003,etSerialNumber2,etInputcuftrev,etUncorrectedRd,etCorrectedRead,etUncorrectedMult,etInstrumentTestData,etPressurerange;
	private RestClient client;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.ert_device_installation, null);
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		client =  HomeActivity.mActivity.client;
		Bean_Class.api_Version = getString(R.string.api_version);
		etCustomer = (EditText)getView().findViewById(R.id.etCustomer);
		etModel1 = (EditText)getView().findViewById(R.id.etmodel);
		etModel2 = (EditText)getView().findViewById(R.id.etmodel2);
		etManufacturer = (EditText)getView().findViewById(R.id.etmanufacturer);
		etMeterInformation = (EditText)getView().findViewById(R.id.etMeterInformation);
		etMeter = (EditText)getView().findViewById(R.id.etMeter);
		etSerialNumber = (EditText)getView().findViewById(R.id.etserialNumber);
		etInspectionDate = (EditText)getView().findViewById(R.id.etinspectionDate);
		etOutputcuftrev = (EditText)getView().findViewById(R.id.etoutputcuftrev);
		etTemperatureComp = (EditText)getView().findViewById(R.id.ettemperatureComp);
		etOriginalMtrSealDate = (EditText)getView().findViewById(R.id.etoriginalMtrSealDate);
		etERTDevice = (EditText)getView().findViewById(R.id.eteRTDevice);
		etMake = (EditText)getView().findViewById(R.id.etMake);
		etModelERG5003 = (EditText)getView().findViewById(R.id.etmodelERG5003);


		etCustomer.setOnClickListener(this);
		etModel1.setOnClickListener(this);
		etModel2.setOnClickListener(this);
		etManufacturer.setOnClickListener(this);

		submit = (Button) getView().findViewById(R.id.submit);
		submit.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.etCustomer:
			AccountLookup acLookUp = new AccountLookup();
			acLookUp.accountLookupDetails(getActivity(), "ERT");
			break;
		case R.id.etmodel:
			ModelLookup modelLookup = new ModelLookup();
			modelLookup.getItemLookUp(getActivity(), "");
			break;
		case R.id.etmodel2:
			ModelLookup modelLookup2 = new ModelLookup();
			modelLookup2.getItemLookUp(getActivity(), "");
			break;
		case R.id.etmanufacturer:
			Log.i("NCS","etmanufacturer is  :");
			ManufacturerLookUp mn = new ManufacturerLookUp();
			mn.getItemLookUp(getActivity(), "");
			break;
		case R.id.submit:
			insertNewERTDevice();
			break;

		default:
			break;
		}
	}


	private void insertNewERTDevice(){



		String objectType = "AMR_Device_Installation__c";                                                                       
		HashMap<String, Object> fields = new HashMap<String, Object>();
		fields.put("Customer__c", Session.sAccountId);
		fields.put("Manufacturer__c", Session.sManufacturerId);
		fields.put("Model1__c", Session.sModelId);
		fields.put("AMR_Device__c",etERTDevice.getText());
		fields.put("Meter__c", etMeter.getText());
		fields.put("Make__c", etMake.getText());

		try {
			RestRequest restRequest = RestRequest.getRequestForCreate(Bean_Class.api_Version,objectType,fields);


			client.sendAsync(restRequest, new AsyncRequestCallback() {
				@Override
				public void onSuccess(RestRequest request, RestResponse result) {
					try {   
						Log.i("NCS","Response is  :"+result);
					}                                
					catch (Exception e) {
						Log.i("TPT","Exception is   :"+e.getMessage());                      
					}
				}

				@Override
				public void onError(Exception exception) {
					EventsObservable.get().notifyEvent(EventType.RenditionComplete);
				}
			});
		} 
		catch (Exception e) {
			Log.i("NCS","Exception is   :"+e.getMessage());

		}



	}


}
